/* util */
import _ from 'lodash';
/* api */
import * as SettingTaskApi from '@src/api/SettingTaskApi';
import { parse } from '@src/util/querystring'
// components
import { openAccurateTab } from '@src/util/platform';
import { PageRoutesTypeEnum } from 'pub-bbx-global/pageType/dist/enum/PageRoutesEnum'
import i18n from '@src/locales'
export default {
  name: 'task_card_setting',
  data() {
    return {
      pending: false,
      loading: false,
      cardData: {},
      cardConfig: {},
      fieldsList:[],
      cardType:'',
      visible: false,
      isError: false
    }
  },
  mounted(){
    let query = parse(window.location.search) || {};
    this.taskCardId = query.cardId;
    this.cardType = query.cardType;
    this.initCardView();
  },
  methods: {
    onSubmit() {
      this.sardTaskCard();
    },
    // 保存物料信息配置
    sardTaskCard() {
      const params = Object.assign(this.cardData, { config: this.cardConfig }) 
      SettingTaskApi.modifyInventoryConfig(params).then(res=>{
        const { code, message, data } = res;
        if(code == 0){
          this.$platform.notification({
            title: i18n.t('common.base.saveSuccess'),
            type: 'success'
          }); 
          setTimeout(()=>{
            this.back()
          }, 500)
        }else{
          this.$platform.notification({
            title: message,
            type: 'error'
          }); 
        }
      }).catch(error=>({}))
    },
    // 返回
    back(){
      let fromId = window.frameElement.getAttribute('fromid');
      let id = window.frameElement.dataset.id
      this.$platform.closeTab(id);
      if(!this.cardType){
        openAccurateTab({
          type: PageRoutesTypeEnum.PageTaskCardManage,
          reload: true,
          fromId
        })
      }
    },
    // 初始化组件信息
    initCardView() {
      SettingTaskApi.getInventoryConfig({taskCardId: this.taskCardId}).then(res=>{
        const { code, result } = res;
        if(code == 0){  
          this.loading = false;
          this.cardData = result;
          this.cardConfig = result.config || {};
          this.fieldsList = _.cloneDeep(result.taskCardFields)
        }
      }).catch(error=>({}))
    },
  },
}